package com.mapper;

import com.entity.Salary;
import com.form.SalaryInfoForAdmin;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 工资表数据层
 *
 * @author z 'y
 */
@Mapper
public interface SalaryMapper {
    /**
     * 查询所有工资记录数量
     *
     * @return the int
     */
    @Select("select count(*) from salary;")
    int countOfSalarys();

    /**
     * 查询所有工资数量，并实现分页
     *
     * @param startIndex the start index
     * @param pageSize   the page size
     * @return the list
     */
    @Select("select fullname,paymentDate,positionSalary,performanceSalary,senioritySalary,allowance,taxation,totalSalaryAmount" +
            ",actualPayAmount from salary natural join employee limit #{param2} offset #{param1}")
    List<SalaryInfoForAdmin> findPage(int startIndex, int pageSize);

    /**
     * 查询某年份和月份下的工资记录
     *
     * @param employeeId   the employee id
     * @param currentYear  the current year
     * @param currentMonth the current month
     * @return the salary
     */
    @Select("select * from salary where employeeId=#{param1} and MONTH(paymentDate) = #{param3} and YEAR(paymentDate)=#{param2}")
    Salary checkCurrentMonthPay(long employeeId, int currentYear, int currentMonth);

    /**
     * 过去某年度的所有工资列表
     *
     * @param employeeId  the employee id
     * @param currentYear the current year
     * @return the total salarys of this year
     */
    @Select("select totalSalaryAmount from salary where  employeeId=#{param1} and Year(paymentDate) = #{param2}")
    List<Double> getTotalSalarysOfThisYear(long employeeId, int currentYear);

    /**
     * 添加工资记录
     *
     * @param salary the salary
     * @return the int
     */
    @Insert("INSERT INTO Salary (employeeId, paymentDate, positionSalary, performanceSalary, senioritySalary, allowance," +
            " taxation, totalSalaryAmount, actualPayAmount) values(#{employeeId},#{paymentDate},#{positionSalary}," +
            "#{performanceSalary},#{senioritySalary},#{allowance},#{taxation},#{totalSalaryAmount},#{actualPayAmount})")
    int addSalary(Salary salary);

    /**
     * 获取关键字对应下的工资记录条数，关键字只能是员工名字
     *
     * @param keys the keys
     * @return the count of salary by keys
     */
    @Select("select count(*) from salary natural join employee where fullName like concat('%',#{keys},'%')")
    int getCountOfSalaryByKeys(String keys);

    /**
     * 查找关键字下的工资记录，并实现分页，关键字只能是员工名字
     *
     * @param keys       the keys
     * @param startIndex the start index
     * @param pageSize   the page size
     * @return the list
     */
    @Select("select username,fullname,paymentDate,positionSalary,performanceSalary,senioritySalary,allowance,taxation,totalSalaryAmount" +
            ",actualPayAmount from salary natural join employee where fullName like concat('%',#{param1},'%') or username " +
            "like concat('%',#{param1},'%') limit #{param3} offset #{param2}")
    List<SalaryInfoForAdmin> findPageByKeys(String keys, int startIndex, int pageSize);
    @Select("select count(*) from salary where employeeId = #{param1} and Month(paymentDate) = #{param3} and " +
            "Year(paymentDate) = #{param2} ")
    int getCountOfSalaryByYearAndMonth(long employeeId, int year, int month);
    @Select("select * from salary where employeeId = #{param1} and Month(paymentDate) = #{param3} and " +
            "Year(paymentDate) = #{param2} limit #{param5} offset #{param4}")
    List<Salary> findPageByYearAndMonth(long employeeId, int year, int month, int startIndex, int pageSize);
    @Select("select count(*) from salary where employeeId = #{param1} and Year(paymentDate) = #{param2} ")
    int countOfYear(long employeeId, int year);
    @Select("select count(*) from salary where employeeId = #{param1} and Month(paymentDate) = #{param2} ")
    int countOfMonth(long employeeId, int month);
    @Select("select * from salary where employeeId = #{param1} and Year(paymentDate) = #{param2} limit #{param4} offset #{param3}")
    List<Salary> findPageByYear(long employeeId, int year, int startIndex, int pageSize);
    @Select("select * from salary where employeeId = #{param1} and Month(paymentDate) = #{param2} limit #{param4} offset #{param3}")
    List<Salary> findPageByMonth(long employeeId, int month, int startIndex, int pageSize);
}
